package net.jeeshop.web.util;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.yuanxin.service.biz.schuserbase.appobject.OrganMemberEntityAO;
import com.yuanxin.service.biz.schuserbase.util.SchUserUtil;

import net.jeeshop.core.FrontContainer;
import net.jeeshop.core.util.AddressUtils;
import net.jeeshop.core.util.MD5;
import net.jeeshop.services.front.account.AccountService;
import net.jeeshop.services.front.account.bean.Account;
import net.jeeshop.services.front.account.bean.LoginTypeEnum;

/**
 * 单点登录工具类
 * @author jxiaolu
 *
 */
public class SingleLoginUtil {
	@Resource
	private AccountService accountService;
	
	/**
	 * 单点登录方法
	 * @param token
	 * @return
	 */
	public Account singleLogin(String token) {
    	Account e = null;
        OrganMemberEntityAO om = SchUserUtil.getOrganMemberEntityAO(token);
        if(StringUtils.isBlank(token) || null == om) {
        	//e = new Account();
        	/*以下代码为开发时使用*/
        	String baseUserId = "3";
        	Account acc = new Account();
        	acc.setBaseuserid(baseUserId);
        	e = accountService.selectOne(acc);
        	/*if(null == e) {//没查询到则插入一条记录，并进行登录
        		e = new Account();
        		e.setAccount(om.getCode());
        		e.setNickname(om.getName());
        		e.setBaseuserid(baseUserId);
        		e.setFreeze("n");
        		e.setEmailIsActive("y");
        		e.setPassword(MD5.md5("123456"));
        		accountService.insert(e);
        	}*/
        	e.setLoginType(LoginTypeEnum.system);//登陆方式
    		RequestHolder.getSession().setAttribute(FrontContainer.USER_INFO, e);
    		//更新用户最后登录时间
    		//e.clear();
    		e.setId(e.getId());
    		e.setLastLoginTime("yes");
    		e.setLastLoginIp(AddressUtils.getIp(RequestHolder.getRequest()));
    		String address = "";
    		try {
    			//address = AddressUtils.getAddresses("ip=" + e.getLastLoginIp(), "utf-8");
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		e.setLastLoginArea(address);
    		accountService.update(e);
        } else {
        	String baseUserId = om.getId();
        	Account acc = new Account();
        	acc.setBaseuserid(baseUserId);
        	e = accountService.selectOne(acc);
        	if(null == e) {//没查询到则插入一条记录，并进行登录
        		e = new Account();
        		e.setAccount(om.getCode());
        		e.setNickname(om.getName());
        		e.setBaseuserid(baseUserId);
        		e.setFreeze("n");
        		e.setEmailIsActive("y");
        		e.setPassword(MD5.md5("123456"));
        		accountService.insertOutAccount(e);
        	}
        	e.setLoginType(LoginTypeEnum.system);//登陆方式
    		RequestHolder.getSession().setAttribute(FrontContainer.USER_INFO, e);
    		//更新用户最后登录时间
    		//e.clear();
    		e.setId(e.getId());
    		e.setLastLoginTime("yes");
    		e.setLastLoginIp(AddressUtils.getIp(RequestHolder.getRequest()));
    		String address = null;
    		try {
    			//address = AddressUtils.getAddresses("ip=" + e.getLastLoginIp(), "utf-8");
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		e.setLastLoginArea(address);
    		accountService.update(e);
        }
		return e;
    }
}
